<?php

namespace app\hotel\controller;

use think\Controller;

class Guest extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->GuestModel = model('Hotel.Guest');
        $this->BusinessModel = model('Business.User');
    }

    // 添加入住人信息
    public function add(){
        if($this->request->isPost()){
            // var_dump($this->request->param());
            // exit;
            $busid = $this->request->param('busid',0,'trim');
            $mobile = $this->request->param('mobile','','trim');
            $nickname = $this->request->param('nickname','','trim');
            $sex = $this->request->param('sex',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户信息未知');
                exit;
            }

            $data = [
                'busid' => $busid,
                'nickname' => $nickname,
                'mobile' => $mobile,
                'sex' => $sex
            ];

            // var_dump($data);
            // exit;

            $result = $this->GuestModel->validate('common/Hotel/Guest')->save($data);

            if($result === FALSE){
                $this->error('添加入住人信息失败');
                exit;
            }else{
                $this->success('添加入住人信息成功');
                exit;
            }
        }
    }


    // 入住人列表
    public function index(){
        if($this->request->isPost()){
            $page = $this->request->param('page',1,'trim');
            $busid = $this->request->param('busid',0,'trim');
            $limit = 10;
            $offset = ($page-1) * $limit;

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户信息未知');
                exit;
            }

            $list = $this->GuestModel
                ->where(['busid' => $busid])
                ->order("id desc")
                ->limit($offset,$limit)
                ->select();

            if($list){
                $this->success('返回住客信息',null,$list);
                exit;
            }else{
                $this->error('暂无住客信息');
                exit;
            }
        }
    }


    // 住客详情
    public function info(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $id = $this->request->param('id',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户信息未知');
                exit;
            }

            $guest = $this->GuestModel->find($id);
            
            if(!$guest){
                $this->error('住客信息未知');
                exit;
            }else{
                $this->success('返回住客信息',null,$guest);
                exit;
            }
        }
    }

    
    // 编辑住户信息
    public function edit(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            $busid = $this->request->param('busid',0,'trim');
            $nickname = $this->request->param('nickname','','trim');
            $mobile = $this->request->param('mobile','','trim');
            $sex = $this->request->param('sex',0,'trim');

            // 查询住户
            $guest = $this->GuestModel->find($id);
            if(!$guest){
                $this->error('住户信息未知');
                exit;
            }

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户信息未知');
                exit;
            }

            $data = [
                'id' => $id,
                'busid' => $busid,
                'nickname' => $nickname,
                'mobile' => $mobile,
                'sex' => $sex
            ];

            $result = $this->GuestModel->isUpdate(true)->save($data);
            if($result === FALSE){
                $this->error('编辑用户失败');
                exit;
            }else{
                $this->success('编辑用户成功');
                exit;
            }
        }
    }


    // 删除住户
    public function del(){
        if($this->request->isPost()){
            $id = $this->request->param('id',0,'trim');
            $busid = $this->request->param('busid',0,'trim');

            // 查询住户
            $guest = $this->GuestModel->find($id);
            if(!$guest){
                $this->error('住户信息未知');
                exit;
            }

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户信息未知');
                exit;
            }

            $result = $this->GuestModel->where(['id' => $id])->delete();
            if($result === FALSE){
                $this->error('删除住户失败');
                exit;
            }else{
                $this->success('删除住户成功');
                exit;
            }
        }
    }

}
